import React, { useState, useCallback, useEffect } from 'react';
import PageHeader from '@/components/page/PageHeader';
import { Select, Table, Space, Switch } from 'antd';
import DataEdit from '@/components/base/DataEdit';
import { InserJxsdzt, SetJxsdcz, fetch } from '@/services/commCRUD';
const daoName = 'Shjhcwjxsd';
const Shjhcwjxsd: React.FC = () => {
  const options: API.OptionItem[] = [];
  const year = new Date();
  const dqyear = year.getFullYear();
  for (let i = dqyear; i >= dqyear - 4; i--) {
    options.push({ label: `${i}年`, value: i });
  }
  const [NF, setNF] = useState<number>(year.getFullYear());
  const [listData, setListData] = useState<Record<string, any>[]>();
  const [load, setLoading] = useState<boolean>();
  const handleChangeNian = (value: number) => {
    setNF(value);
  };
  const handleSwitchChange = (row: Record<string, any>, zt: boolean) => {
    const updatedListData = listData!.map((item) => (item === row ? { ...item, CZ: zt } : item));
    setListData(updatedListData);
    SetJxsdcz(NF, row.ID, zt);
  };
  const columns: API.Column[] = [
    {
      title: '时间',
      width: 150,
      sorter: true,
      dataIndex: 'SJ',
      key: 'SJ',
      dispaly: true,
      filter: true,
      export: true,
    },
    {
      title: '操作',
      width: 150,
      sorter: true,
      key: 'CZ',
      render: (record: any) => (
        <Space direction="vertical" key="submit">
          <Switch
            checkedChildren="开启"
            unCheckedChildren="关闭"
            checked={record.CZ}
            onChange={(zt) => handleSwitchChange(record, zt)}
          />
        </Space>
      ),
      dispaly: true,
      filter: true,
      export: true,
    },
  ];

  const loadData = useCallback(() => {
    setLoading(true);
    const order: API.OrderItem = {
      FieldName: 'ID',
      Direction: 'ASC',
    };
    const seeks: API.WhereCause[] = [
      {
        WhereItems: [
          {
            FieldName: 'NIAN',
            Operator: '=',
            FieldValues: '' + NF,
          },
        ],
        RelationSign: 'AND',
      },
    ];
    fetch(daoName, 1, 50, [order], seeks, 0)
      .then(function (data) {
        if (data.Objectlist.length > 0) {
          setListData(data.Objectlist);
        } else {
          InserJxsdzt(NF);
        }
      })
      .catch(function (ex) {
        console.error('请求', daoName, '错误', ex);
      })
      .finally(function () {
        setLoading(false);
      });
  }, [NF]);
  useEffect(() => {
    loadData();
  }, [NF, loadData]);

  return (
    <PageHeader>
      <div style={{ marginTop: -8, marginBottom: 5 }}>
        <DataEdit
          showType="inner"
          layout="inline"
          renderButtons={() => {
            return [
              <>
                <h4 style={{ marginTop: 5, marginBottom: 10, marginLeft: 10, marginRight: 10 }}>
                  年份:
                </h4>
                <Select value={NF} onChange={handleChangeNian} options={options} key="submit" />
              </>,
            ];
          }}
          controls={[]}
        />
      </div>
      <Table
        columns={columns}
        dataSource={listData}
        loading={load}
        rowKey={(record) => record.ID}
        bordered={true}
        pagination={false}
        size="small"
      />
    </PageHeader>
  );
};
export default Shjhcwjxsd;
